package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.dao.CourierStationModifyDao;
import com.hualan.dao.impl.CourierStationModifyDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/courierStation/modifyCourierStation")
public class CourierStationModifyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int parameterCount = 0;
        int row = 0;
        // 创建courierStationModifyDao对象
        CourierStationModifyDao courierStationModifyDao = new CourierStationModifyDaoImpl();
        // 计算前端传来的参数有几个
        Enumeration<String> parameterNames = req.getParameterNames();
        while(parameterNames.hasMoreElements()) {
            parameterNames.nextElement();
            parameterCount++;
        }

        // 设置响应对象response  可以给前端传递中文数据
        Map<String, Object> map = new HashMap<>();

        // 如果参数多于2是修改
        if (parameterCount > 2) {
            // 接收修改数据
            String courierStationId = req.getParameter("courierStationId");
            String courierStationAddress = req.getParameter("courierStationAddress");
            String courierStationCity = req.getParameter("courierStationCity");
            String courierStationCommunityId = req.getParameter("courierStationCommunityId");
            String courierStationDistrict = req.getParameter("courierStationDistrict");
            String courierStationImage = req.getParameter("courierStationImage");
            String courierStationName = req.getParameter("courierStationName");
            String courierStationProvince = req.getParameter("courierStationProvince");

            // 判断小区id为空
            if (courierStationCommunityId == null || "".equals(courierStationCommunityId)) {
                courierStationCommunityId = "1";
            }

            System.out.println("后端收到编辑小区的数据" + courierStationCity);
            System.out.println("后端收到编辑小区的数据" + courierStationId);

            row = courierStationModifyDao.modifyCourierStation(courierStationAddress, courierStationCity, courierStationCommunityId, courierStationDistrict, courierStationImage, courierStationName, courierStationProvince, courierStationId);
        }else {
            String courierStationId = req.getParameter("courierStationId");
            String courierStationStatus = req.getParameter("courierStationStatus");

            System.out.println("后端收到的修改上下架数据" + courierStationId);
            // 创建courierStationModifyDao对象
            row = courierStationModifyDao.putOrDownCourierStation(courierStationId, courierStationStatus);
        }

        if (row < 1){
            map.put("code", 302);
            map.put("message", "修改失败");
            return;
        }else {
            map.put("code", 200);
            map.put("message", "修改成功");
        }
        resp.getWriter().write(JSON.toJSONString(map));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
